//
//  DHS2_SearchView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/6.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView.h"
#import "DHS2_SearchView_All.h"
#import "DHS2_SearchView_Products.h"
#import "DHS2_SearchView_Brand.h"
#import "DHS2_SearchView_News.h"
#import "DHS2_SearchView_Community.h"

@interface DHS2_SearchView()

@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewleftLayout;

@property (strong, nonatomic) NSString *keyWord; //关键字

@property (strong, nonatomic) DHS2_SearchView_All *allView;
@property (strong, nonatomic) DHS2_SearchView_Products *productsView;
@property (strong, nonatomic) DHS2_SearchView_Brand *brandsView;
@property (strong, nonatomic) DHS2_SearchView_News *newsView;
@property (strong, nonatomic) DHS2_SearchView_Community *communityView;

@end

@implementation DHS2_SearchView

+ (DHS2_SearchView *)shareDHS2_SearchView{
    DHS2_SearchView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)initView{
    self.lineViewWidth.constant = (kScreenWidth / 5) / 2;
    self.lineViewleftLayout.constant = (kScreenWidth / 5) / 4;
    
    self.allView = [DHS2_SearchView_All shareDHS2_SearchView_All];
    self.allView.frame = CGRectMake(0, 35, self.width, self.height - 35);
    [self addSubview:self.allView];
    
    self.productsView = [DHS2_SearchView_Products shareDHS2_SearchView_Products];
    self.productsView.frame = CGRectMake(0, 35, self.width, self.height - 35);
    [self addSubview:self.productsView];
    
    self.brandsView = [DHS2_SearchView_Brand shareDHS2_SearchView_Brand];
    self.brandsView.frame = CGRectMake(0, 35, self.width, self.height - 35);
    [self addSubview:self.brandsView];
    
    self.newsView = [DHS2_SearchView_News shareDHS2_SearchView_News];
    self.newsView.frame = CGRectMake(0, 35, self.width, self.height - 35);
    [self addSubview:self.newsView];
    
    self.communityView = [DHS2_SearchView_Community shareDHS2_SearchView_Community];
    self.communityView.frame = CGRectMake(0, 35, self.width, self.height - 35);
    [self addSubview:self.communityView];
    
    self.productsView.hidden = YES;
    self.brandsView.hidden = YES;
    self.newsView.hidden = YES;
    self.communityView.hidden= YES;
}

- (void)searchAction:(NSString *)keyWord{
    self.keyWord = keyWord;

    if ([self.type isEqualToString:@"全部"]) {
        self.allView.keyWord = keyWord;
        [self.allView searchData];
    }else if ([self.type isEqualToString:@"商品"]){
        self.productsView.keyWord = self.keyWord;
        [self.productsView searchData];
    }else if ([self.type isEqualToString:@"品牌"]){
        self.brandsView.keyWord = self.keyWord;
        [self.brandsView searchData];
    }else if ([self.type isEqualToString:@"资讯"]){
        self.newsView.keyWord = self.keyWord;
        [self.newsView searchData];
    }else if ([self.type isEqualToString:@"社区"]){
        self.communityView.keyWord = self.keyWord;
        [self.communityView searchData];
    }
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"product_all"]) {
        self.lineView.centerX = (kScreenWidth / 5) * 1 + (kScreenWidth / 5) / 2;
        self.type = @"商品";
        self.allView.hidden = YES;
        self.productsView.hidden = NO;
        self.brandsView.hidden = YES;
        self.newsView.hidden = YES;
        self.communityView.hidden = YES;
        
        self.productsView.keyWord = self.keyWord;
        [self.productsView searchData];
    }else if ([eventName isEqualToString:@"brand_all"]){
        self.lineView.centerX = (kScreenWidth / 5) * 2 + (kScreenWidth / 5) / 2;
        self.type = @"品牌";
        self.allView.hidden = YES;
        self.productsView.hidden = YES;
        self.brandsView.hidden = NO;
        self.newsView.hidden = YES;
        self.communityView.hidden = YES;
        
        self.brandsView.keyWord = self.keyWord;
        [self.brandsView searchData];
    }else if ([eventName isEqualToString:@"news_all"]){
        self.lineView.centerX = (kScreenWidth / 5) * 3 + (kScreenWidth / 5) / 2;
        self.type = @"资讯";
        
        self.allView.hidden = YES;
        self.productsView.hidden = YES;
        self.brandsView.hidden = YES;
        self.newsView.hidden = NO;
        self.communityView.hidden = YES;
        
        self.newsView.keyWord = self.keyWord;
        [self.newsView searchData];
    }else if ([eventName isEqualToString:@"cou_all"]){
        self.lineView.centerX = (kScreenWidth / 5) * 4 + (kScreenWidth / 5) / 2;
        self.type = @"社区";
        
        self.allView.hidden = YES;
        self.productsView.hidden = YES;
        self.brandsView.hidden = YES;
        self.newsView.hidden = YES;
        self.communityView.hidden = NO;
        
        self.communityView.keyWord = self.keyWord;
        [self.communityView searchData];
    }
}

- (IBAction)tapAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    self.lineView.centerX = btn.centerX;
    switch (btn.tag) {
        case 1000:{
            self.type = @"全部";
            self.allView.hidden = NO;
            self.productsView.hidden = YES;
            self.brandsView.hidden = YES;
            self.newsView.hidden = YES;
            self.communityView.hidden = YES;
            
            self.allView.keyWord = self.keyWord;
            [self.allView searchData];
            break;
        }
        case 1001:{
            self.type = @"商品";
            self.allView.hidden = YES;
            self.productsView.hidden = NO;
            self.brandsView.hidden = YES;
            self.newsView.hidden = YES;
            self.communityView.hidden = YES;
            
            self.productsView.keyWord = self.keyWord;
            [self.productsView searchData];
            break;
        }
        case 1002:{
            self.type = @"品牌";
            self.allView.hidden = YES;
            self.productsView.hidden = YES;
            self.brandsView.hidden = NO;
            self.newsView.hidden = YES;
            self.communityView.hidden = YES;
            
            self.brandsView.keyWord = self.keyWord;
            [self.brandsView searchData];
            break;
        }
        case 1003:{
            self.type = @"资讯";
            
            self.allView.hidden = YES;
            self.productsView.hidden = YES;
            self.brandsView.hidden = YES;
            self.newsView.hidden = NO;
            self.communityView.hidden = YES;
            
            self.newsView.keyWord = self.keyWord;
            [self.newsView searchData];
            break;
        }
        case 1004:{
            self.type = @"社区";
            
            self.allView.hidden = YES;
            self.productsView.hidden = YES;
            self.brandsView.hidden = YES;
            self.newsView.hidden = YES;
            self.communityView.hidden = NO;
            
            self.communityView.keyWord = self.keyWord;
            [self.communityView searchData];
            break;
        }
            
        default:
            break;
    }
}

@end
